// Fill out your copyright notice in the Description page of Project Settings.


#include "StartGameMode.h"
#include "StartUserWidget.h"
#include "StartPlayerController.h"
#include "Blueprint/UserWidget.h"
#include "Components/Button.h"
#include "Components/EditableTextBox.h"

// 构造方法
AStartGameMode::AStartGameMode()
{
	// 设置游戏PlayerController
	PlayerControllerClass = AStartPlayerController::StaticClass();
}

// 游戏开始调用事件
void AStartGameMode::BeginPlay()
{
	// Super::BeginPlay();

	// 初始化开始界面
	StartWidget = CreateWidget<UStartUserWidget>(GetGameInstance(), LoadClass<UStartUserWidget>(this, TEXT("WidgetBlueprint'/Game/StartWidget.StartWidget_C'")));

	// 开始界面加入视口
	StartWidget->AddToViewport();

	// 创建文件按钮绑定事件
	StartWidget->CreateFileButton->OnClicked.AddDynamic(this, &AStartGameMode::CreateFileButtonClickedEvent);

	// 写入Game.ini文件按钮绑定事件
	StartWidget->WriteGameIniButton->OnClicked.AddDynamic(this, &AStartGameMode::WriteGameIniButtonClickedEvent);

	// 读取Game.ini按钮钮绑定事件
	StartWidget->ReadGameIniButton->OnClicked.AddDynamic(this, &AStartGameMode::ReadGameIniButtonClickedEvent);
}

// 创建文件按钮点击事件
void AStartGameMode::CreateFileButtonClickedEvent()
{
	/**
	 * 写入自定义文件
	 */
	// 读取需要写入文件的内容
	FString CreteFileTexts = StartWidget->CrateTextBox->GetText().ToString();
	UE_LOG(LogTemp, Warning,  TEXT("%s"), *CreteFileTexts);

	// 路径拼接
	FString FilePath = FPaths::Combine(FPaths::ProjectSavedDir(), TEXT("Game.ini"));
	
	// FString FilePath = FString("D:/SaveStr.txt");
	FFileHelper::SaveStringToFile(CreteFileTexts, *FilePath);
	
	// if (GEngine)
	// {
	// 	GEngine->AddOnScreenDebugMessage(-1,2,FColor::Yellow, TEXT("CreateFileButtonClickedEvent"));
	// }
}

// 写入Game.ini按钮点击事件
void AStartGameMode::WriteGameIniButtonClickedEvent()
{
	/**
	 * 写入配置文件
	 */
	if(GConfig)
	{
		FString Name = "Duck";
		TArray<FString> Buff;
		int32 DeadTimes = 100;
		// Buff.Add(TEXT("Chicken"));
		// Buff.Add(TEXT("Strong"));
		// Buff.Add(TEXT("Speed"));

		GConfig->SetString(TEXT("DarkSoul"), TEXT("Name"), *Name, GGameIni);
		GConfig->SetInt(TEXT("DarkSoul"), TEXT("DeadTimes"), DeadTimes, GGameIni);
		// GConfig->SetArray(TEXT("DarkSoul"), TEXT("Buff"), Buff, GGameIni);
	}
}

// 读取Game.ini按钮点击事件
void AStartGameMode::ReadGameIniButtonClickedEvent()
{
	/**
	 * 读取配置文件
	 */
	if(GConfig)
	{
		FString Name;
		int32 DeadTimes;

		bool succee = GConfig->GetString(TEXT("DarkSoul"), TEXT("Name"), Name, GGameIni);
		succee &= GConfig->GetInt(TEXT("DarkSoul"), TEXT("DeadTimes"), DeadTimes, GGameIni);
		if(succee)
		{
			UE_LOG(LogTemp, Warning, TEXT(" Name: %s, DeadTimes: %d"), *Name, DeadTimes);
		}
	}
	
	
}
